package com.sgb.link.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.link.R;
import com.sgb.link.utils.JiCaiEditInputFilter;
import com.sgb.link.utils.UnitListUtils;
import com.swgk.core.util.MToast;

public class EditJcNumPopWindow {
    private Context context;
    private Dialog dialog;
    private Display display;

    private LinearLayout ll_content;
    private EditText etNum;
    private TextView tvCancel;
    private TextView tvQueue;
    private EditNumCallBack editNumCallBack;
    private String UnitNames = "";

    public EditJcNumPopWindow(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public EditJcNumPopWindow builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(R.layout.layout_popwindow_edit_num, null);
        // 获取自定义Dialog布局中的控件
        ll_content = view.findViewById(R.id.ll_content);
        etNum = view.findViewById(R.id.et_num);

        tvCancel = view.findViewById(R.id.tv_cancel);
        tvQueue = view.findViewById(R.id.tv_queue);
        // 定义Dialog布局和参数
        dialog = new Dialog(context, com.swgk.core.R.style.AlertDialogStyle);
        dialog.setContentView(view);
        // 调整dialog背景大小
        ll_content.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.8), LinearLayout.LayoutParams.WRAP_CONTENT));
        return this;
    }

    public EditJcNumPopWindow setTvNum(String textNum,String UnitName) {
        etNum.setText(textNum);
        UnitNames = UnitName;
        //小数点后保留两位数字
        if (UnitListUtils.getUnitStart(UnitNames)){
            etNum.setFilters(new JiCaiEditInputFilter[]{new JiCaiEditInputFilter(999999.999, 0.001,3)});
        }else {
            etNum.setFilters(new JiCaiEditInputFilter[]{new JiCaiEditInputFilter(999999, 1,0)});
        }
        return this;
    }

    private void setLayout() {
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tvQueue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num = etNum.getText().toString().trim();
                if (num.equals("")) {
                    MToast.showToast(context, "请填写数字");
                    return;
                }
                if (null != editNumCallBack) {
                    editNumCallBack.editNum(num);
                }
                dialog.dismiss();
            }
        });
    }

    public void show() {
        setLayout();
     /*   dotReservedTwo();*/
        dialog.show();
    }

    /**
     * EditText保留小数点后两位
     */
    private void dotReservedTwo() {
        etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {

                //删除“.”后面超过2位后的数据
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        etNum.setText(s);
                        etNum.setSelection(s.length()); //光标移到最后
                    }
                }
                //如果"."在起始位置,则起始位置自动补0
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    etNum.setText(s);
                    etNum.setSelection(2);
                }

                //如果起始位置为0,且第二位跟的不是".",则无法后续输入
                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        etNum.setText(s.subSequence(0, 1));
                        etNum.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }

        });

    }


    public EditJcNumPopWindow setEditNumCallBack(EditNumCallBack editNumCallBack) {
        this.editNumCallBack = editNumCallBack;
        return this;
    }

    public interface EditNumCallBack {
        void editNum(String num);
    }

}
